package com.exam.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.exam.entity.Marker;
import com.exam.entity.TCase;


public class MarkerDaoPgImpl extends BaseDao implements MarkerDao, PostgreSQL
{
	public List<Marker> listMarker(Date beginDate, Date endDate, boolean flag) throws Exception
	{
		List<Marker> list = new ArrayList<Marker>();
		String sql = "select * from t_marker ";
		if (beginDate != null && endDate != null)
		{
			sql += "where flag = ? and date between ? and ?";
			list = esql.list(Marker.class, sql, flag, beginDate, endDate);
			return list;
		}
		list = esql.list(Marker.class, sql);
		return list;
	}

	@Override
	public long addMarker(Marker marker) throws Exception
	{
		if (esql.query(Marker.class, "select * from t_marker where identify = ?", marker.getIdentify()) != null)
			return 0;
		esql.helper().insert(marker, "t_marker", null, "id");
		System.out.println("成功导入" + marker.getAddress());
		return getGeneratedId("t_marker");
	}

	@Override
	public Marker getMarkerById(long id) throws Exception
	{
		return esql.helper().query(Marker.class, "t_marker", null, null, "id = ?", id);
	}

	@Override
	public TCase getCaseById(long identify) throws Exception
	{
		return esql.helper().query(TCase.class, "t_case", null, null, "identify = ?", identify);
	}

	@Override
	public Marker getMarkerBySearchCondition(Long id, String name) throws Exception
	{
		String sql = "select t_marker.* from t_marker, t_case where t_marker.identify = t_case.identify ";
		List<Object> parameter = new ArrayList<Object>();
		if (id != null)
		{
			sql += "and t_marker.identify = ? ";
			parameter.add(id);
		}
		if (name != null && !name.equals(""))
		{
			sql += "and t_case.hzname like ?";
			parameter.add("%%" + name + "%%");
		}
		
		return esql.query(Marker.class, sql, parameter.toArray());
	}
}
